clear
clear mata
clear matrix
capture log close
program drop _all
macro drop _all
version 10.0
set mem 500m
set mat 2000
set more off
cd "E:\REStat_MS14767_Vol96(2)\Estimation"
log using "REStat_MS14797_Vol96(2)_estimation_compustat_segment.log", replace


********************************************************************************
********                                                ************************
********      in this file we do the main regressions   ************************
********                                                ************************
********************************************************************************


use RESTATestimation_sample_segment.dta, clear
xtset obs_num year


***** UNIT ROOTS TESTS
egen a=count(obs_num), by(obs_num)

*xtunitroot fisher MS if zero_same==0 &  network>=0 & a>3, pperron lags(1)
*xtunitroot fisher MS if zero_same==0 &  network>=0 & a>3, dfuller lags(1)


*************************************************************
**** preliminary statistics
*************************************************************


replace networkMS=0 if networkMS==.
gen RJVhorMS_s=0
replace RJVhorMS_s=1 if networkMS>0 & networkMS<.0676542

gen RJVhorMS_m=0
replace RJVhorMS_m=1 if networkMS>=.0676542 & networkMS<.3846659

gen RJVhorMS_l=0
replace RJVhorMS_l=1 if networkMS>=.3846659

tabstat MS sales_new ta rd2 rd_int patents pat_corr network networkMS coverage RJVsametot links2_ver m_rdexpense m_lta ncomp if RJV2==0 & zero_same==1, stat (mean sd n) c(st)
tabstat MS sales_new ta rd2 rd_int patents pat_corr network networkMS coverage RJVsametot links2_ver m_rdexpense m_lta ncomp if RJV2==1, stat (mean sd n) c(st)
tabstat MS sales_new ta rd2 rd_int patents pat_corr network networkMS coverage RJVsametot links2_ver m_rdexpense m_lta ncomp if RJVver==1, stat (mean sd n) c(st)
tabstat MS sales_new ta rd2 rd_int patents pat_corr network networkMS coverage RJVsametot links2_ver m_rdexpense m_lta ncomp if RJVver_sure==1, stat (mean sd n) c(st)
tabstat MS sales_new ta rd2 rd_int patents pat_corr network networkMS coverage RJVsametot links2_ver m_rdexpense m_lta ncomp if RJVver_nsure==1, stat (mean sd n) c(st)
tabstat MS sales_new ta rd2 rd_int patents pat_corr network networkMS coverage RJVsametot links2_ver m_rdexpense m_lta ncomp if RJVhor==1, stat (mean sd n) c(st)


tabstat MS  sales_new ta rd2 rd_int patents pat_corr network networkMS coverage RJVsametot links2_ver m_rdexpense m_lta ncomp if RJVhor==1 & (network==0 | network<.0267857), stat (mean sd n) c(st)
tabstat MS  sales_new ta rd2 rd_int patents pat_corr network networkMS coverage RJVsametot links2_ver m_rdexpense m_lta ncomp if RJVhor==1 & (network==0 | network>.0267857 & network<.13),  stat (mean sd n) c(st)
tabstat MS  sales_new ta rd2 rd_int patents pat_corr network networkMS coverage RJVsametot links2_ver m_rdexpense m_lta ncomp if RJVhor==1 & (network==0 | network>.13), stat (mean sd n) c(st)

tabstat networkMS if RJVhor==1 & (networkMS==0 | networkMS<.0676542), stat (mean sd n) c(st)
tabstat networkMS if networkMS>=.0676542 & networkMS<.3846659, stat (mean sd n) c(st)
tabstat networkMS if networkMS>=.3846659, stat (mean sd n) c(st)

*************************************************************
**** REGRESSIONS
*************************************************************

* here we make the (extreme) assumption that the same firm in different
* indusutries are different observations
* horizontal and vertical RJVs are  defined accordingly

* some preliminary table

tab RJV2 if zero_same==0
tab year RJV2 if zero_same==0, row
tab RJVhor RJVver if zero_same==0, row col

************************
*********  RJV ANY
************************


xtdpd L(0/1).MS L(0/2).RJV2 L(0/2).lrd2 L(1/1).m_lta L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(3 5)) dgmmiv(RJV2, lag(3 5)) dgmmiv(lrd2, lag(5 5))  lgmmiv(l(3/6).MS l(3/6).lrd2)  div(patents_t_2 l2.ta) div(L(1/1).m_lta L(1/1).m_lrd) twostep
estat sargan
*no system
xtdpd L(0/1).MS L(0/2).RJV2 L(0/2).lrd2 L(1/1).m_lta L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(3 5)) dgmmiv(RJV2, lag(3 5)) dgmmiv(lrd2, lag(5 5))  div(patents_t_2 l2.ta) div(L(1/1).m_lta L(1/1).m_lrd) twostep
estat sargan
xtdpd L(0/1).MS L(0/2).RJV2 L(0/2).lrd2 L(1/1).m_lta L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(3 5)) dgmmiv(RJV2, lag(3 5)) dgmmiv(lrd2, lag(5 5))  lgmmiv(l(3/6).MS l(3/6).lrd2)  div(patents_t_2 l2.ta) div(L(1/1).m_lta L(1/1).m_lrd) twostep vce(r)
estat abond
eststo RJVseg_any
lincom RJV2+L1.RJV2+l2.RJV2
lincom lrd2+L1.lrd2+l2.lrd2

displ chi2(60,55)

tab RJV2 if e(sample)
tab year RJV2 if e(sample), row
tab RJVhor RJVver if e(sample), row col


************************
*********  HORIZONZAL vs. VERTICAL RJV
************************

xtdpd L(0/1).MS L(0/2).RJVhor L(0/2).RJVver_sure L(0/2).RJVver_nsure L(0/2).lrd2 L(1/1).m_lta L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(3 5)) dgmmiv(RJVhor, lag(3 5))  dgmmiv(RJVver_sure, lag(3 5)) dgmmiv(RJVver_nsure, lag(3 5)) dgmmiv(lrd2, lag(5 5))  lgmmiv(l(3/6).MS l(3/6).lrd2)  div(patents_t_2 l2.ta) div(L(1/1).m_lta L(1/1).m_lrd) twostep
estat sargan
*no system
xtdpd L(0/1).MS L(0/2).RJVhor L(0/2).RJVver_sure L(0/2).RJVver_nsure L(0/2).lrd2 L(1/1).m_lta L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(3 5)) dgmmiv(RJVhor, lag(3 5))  dgmmiv(RJVver_sure, lag(3 5)) dgmmiv(RJVver_nsure, lag(3 5)) dgmmiv(lrd2, lag(5 5))  div(patents_t_2 l2.ta) div(L(1/1).m_lta L(1/1).m_lrd) twostep
estat sargan
xtdpd L(0/1).MS L(0/2).RJVhor L(0/2).RJVver_sure L(0/2).RJVver_nsure L(0/2).lrd2 L(1/1).m_lta L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(3 5)) dgmmiv(RJVhor, lag(3 5))  dgmmiv(RJVver_sure, lag(3 5)) dgmmiv(RJVver_nsure, lag(3 5)) dgmmiv(lrd2, lag(5 5))  lgmmiv(l(3/6).MS l(3/6).lrd2)  div(patents_t_2 l2.ta) div(L(1/1).m_lta L(1/1).m_lrd) twostep vce(r)
estat abond
eststo RJVseg_hv
lincom RJVhor+L1.RJVhor+l2.RJVhor
lincom RJVver_sure+L1.RJVver_sure+l2.RJVver_sure
lincom RJVver_nsure+L1.RJVver_nsure+l2.RJVver_nsure
lincom lrd2+L1.lrd2+l2.lrd2

displ chi2(50,54)



***********************
**** HORIZONTAL SMALL - MEDIUM - LARGE vs VERTICAL RJV
***********************

* regressions with overlap
xtdpd L(0/1).MS L(0/2).RJVhor_s L(0/2).RJVhor_m L(0/2).RJVhor_l L(0/2).RJVver_sure L(0/2).RJVver_nsure L(0/2).lrd2 L(1/1).m_lta L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(2 5)) dgmmiv(RJVhor_s, lag(2 5))  dgmmiv(RJVhor_m, lag(2 5))  dgmmiv(RJVhor_l, lag(2 5)) dgmmiv(RJVver_sure, lag(2 5)) dgmmiv(RJVver_nsure, lag(2 5)) dgmmiv(lrd2, lag(2 5))  lgmmiv(l(3/6).MS l(3/6).lrd2)  div(patents_t_2 l2.sales_new) div(L(1/1).m_lta L(1/1).m_lrd dy2-dy13) twostep 
estat sargan
*no system
xtdpd L(0/1).MS L(0/2).RJVhor_s L(0/2).RJVhor_m L(0/2).RJVhor_l L(0/2).RJVver_sure L(0/2).RJVver_nsure L(0/2).lrd2 L(1/1).m_lta L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(2 5)) dgmmiv(RJVhor_s, lag(2 5))  dgmmiv(RJVhor_m, lag(2 5))  dgmmiv(RJVhor_l, lag(2 5)) dgmmiv(RJVver_sure, lag(2 5)) dgmmiv(RJVver_nsure, lag(2 5)) dgmmiv(lrd2, lag(2 5))  div(patents_t_2 l2.sales_new) div(L(1/1).m_lta L(1/1).m_lrd dy2-dy13) twostep 
estat sargan
xtdpd L(0/1).MS L(0/2).RJVhor_s L(0/2).RJVhor_m L(0/2).RJVhor_l L(0/2).RJVver_sure L(0/2).RJVver_nsure L(0/2).lrd2 L(1/1).m_lta L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(2 5)) dgmmiv(RJVhor_s, lag(2 5))  dgmmiv(RJVhor_m, lag(2 5))  dgmmiv(RJVhor_l, lag(2 5)) dgmmiv(RJVver_sure, lag(2 5)) dgmmiv(RJVver_nsure, lag(2 5)) dgmmiv(lrd2, lag(2 5))  lgmmiv(l(3/6).MS l(3/6).lrd2)  div(patents_t_2 l2.sales_new) div(L(1/1).m_lta L(1/1).m_lrd dy2-dy13) twostep vce(r)
estat abond
eststo RJVseg_sml
lincom RJVhor_s+L1.RJVhor_s+l2.RJVhor_s
lincom RJVhor_m+L1.RJVhor_m+l2.RJVhor_m
lincom RJVhor_l+L1.RJVhor_l+l2.RJVhor_l
lincom RJVver_sure+L1.RJVver_sure+l2.RJVver_sure
lincom RJVver_nsure+L1.RJVver_nsure+l2.RJVver_nsure
lincom lrd2+L1.lrd2+l2.lrd2

displ chi2(60,66)


***********************
**** HORIZONTAL SMALL - MEDIUM - LARGE vs VERTICAL RJV
****    network based on MS
***********************


xtdpd L(0/1).MS L(0/2).RJVhorMS_s L(0/2).RJVhorMS_m L(0/2).RJVhorMS_l L(0/2).RJVver_sure L(0/2).RJVver_nsure L(0/2).lrd2 L(1/1).m_lta L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(2 5)) dgmmiv(RJVhorMS_s, lag(2 5))  dgmmiv(RJVhorMS_m, lag(2 5))  dgmmiv(RJVhorMS_l, lag(2 5)) dgmmiv(RJVver_sure, lag(2 5)) dgmmiv(RJVver_nsure, lag(2 5)) dgmmiv(lrd2, lag(2 5))  lgmmiv(l(3/6).MS l(3/6).lrd2)  div(patents_t_2 l2.sales_new) div(L(1/1).m_lta L(1/1).m_lrd dy2-dy13) twostep 
estat sargan
*no system
xtdpd L(0/1).MS L(0/2).RJVhorMS_s L(0/2).RJVhorMS_m L(0/2).RJVhorMS_l L(0/2).RJVver_sure L(0/2).RJVver_nsure L(0/2).lrd2 L(1/1).m_lta L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(2 5)) dgmmiv(RJVhorMS_s, lag(2 5))  dgmmiv(RJVhorMS_m, lag(2 5))  dgmmiv(RJVhorMS_l, lag(2 5)) dgmmiv(RJVver_sure, lag(2 5)) dgmmiv(RJVver_nsure, lag(2 5)) dgmmiv(lrd2, lag(2 5))  div(patents_t_2 l2.sales_new) div(L(1/1).m_lta L(1/1).m_lrd dy2-dy13) twostep 
estat sargan
xtdpd L(0/1).MS L(0/2).RJVhorMS_s L(0/2).RJVhorMS_m L(0/2).RJVhorMS_l L(0/2).RJVver_sure L(0/2).RJVver_nsure L(0/2).lrd2 L(1/1).m_lta L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(2 5)) dgmmiv(RJVhorMS_s, lag(2 5))  dgmmiv(RJVhorMS_m, lag(2 5))  dgmmiv(RJVhorMS_l, lag(2 5)) dgmmiv(RJVver_sure, lag(2 5)) dgmmiv(RJVver_nsure, lag(2 5)) dgmmiv(lrd2, lag(2 5))  lgmmiv(l(3/6).MS l(3/6).lrd2)  div(patents_t_2 l2.sales_new) div(L(1/1).m_lta L(1/1).m_lrd dy2-dy13) twostep vce(r)
estat abond
eststo RJVseg_sml_ms
lincom RJVhorMS_s+L1.RJVhorMS_s+l2.RJVhorMS_s
lincom RJVhorMS_m+L1.RJVhorMS_m+l2.RJVhorMS_m
lincom RJVhorMS_l+L1.RJVhorMS_l+l2.RJVhorMS_l
lincom RJVver_sure+L1.RJVver_sure+l2.RJVver_sure
lincom RJVver_nsure+L1.RJVver_nsure+l2.RJVver_nsure
lincom lrd2+L1.lrd2+l2.lrd2

displ chi2(60,75)


******
*** VERTICAL SMALL - MEDIUM - LARGE
******

gen RJVver_s=0
replace  RJVver_s= RJVver_sure if links2_ver<=8
gen RJVver_m=0
replace  RJVver_m= RJVver_sure if links2_ver>8 & links2_ver<=57
gen RJVver_l=0
replace  RJVver_l= RJVver_sure if links2_ver>57



xtdpd L(0/1).MS L(0/2).RJVhor L(0/2).RJVver_s L(0/2).RJVver_m L(0/2).RJVver_l L(0/2).RJVver_nsure L(0/2).lrd2 L(1/1).m_lta L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(3 5)) dgmmiv(RJVhor, lag(3 5)) dgmmiv(RJVver_s, lag(3 5))  dgmmiv(RJVver_m, lag(2 5))  dgmmiv(RJVver_l, lag(3 5)) dgmmiv(RJVver_nsure, lag(3 5)) dgmmiv(lrd2, lag(3 5))  lgmmiv(l(3/6).MS l(3/6).lrd2)  div(patents_t_2 l2.sales_new) div(L(1/1).m_lta L(1/1).m_lrd dy2-dy13) twostep
estat sargan
*no system
xtdpd L(0/1).MS L(0/2).RJVhor L(0/2).RJVver_s L(0/2).RJVver_m L(0/2).RJVver_l L(0/2).RJVver_nsure L(0/2).lrd2 L(1/1).m_lta L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(3 5)) dgmmiv(RJVhor, lag(3 5)) dgmmiv(RJVver_s, lag(3 5))  dgmmiv(RJVver_m, lag(2 5))  dgmmiv(RJVver_l, lag(3 5)) dgmmiv(RJVver_nsure, lag(3 5)) dgmmiv(lrd2, lag(3 5))  div(patents_t_2 l2.sales_new) div(L(1/1).m_lta L(1/1).m_lrd dy2-dy13) twostep
estat sargan
xtdpd L(0/1).MS L(0/2).RJVhor L(0/2).RJVver_s L(0/2).RJVver_m L(0/2).RJVver_l L(0/2).RJVver_nsure L(0/2).lrd2 L(1/1).m_lta L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(3 5)) dgmmiv(RJVhor, lag(3 5)) dgmmiv(RJVver_s, lag(3 5))  dgmmiv(RJVver_m, lag(2 5))  dgmmiv(RJVver_l, lag(3 5)) dgmmiv(RJVver_nsure, lag(3 5)) dgmmiv(lrd2, lag(3 5))  lgmmiv(l(3/6).MS l(3/6).lrd2)  div(patents_t_2 l2.sales_new) div(L(1/1).m_lta L(1/1).m_lrd dy2-dy13) twostep vce(r)
estat abond
eststo RJVseg_ver_sml
lincom RJVhor+L1.RJVhor+l2.RJVhor
lincom RJVver_s+L1.RJVver_s+l2.RJVver_s
lincom RJVver_m+L1.RJVver_m+l2.RJVver_m
lincom RJVver_l+L1.RJVver_l+l2.RJVver_l
lincom RJVver_nsure+L1.RJVver_nsure+l2.RJVver_nsure
lincom lrd2+L1.lrd2+l2.lrd2

displ chi2(60,71)

***********************
* continuous effect
***********************

xtdpd L(0/1).MS L(0/2).network L(0/2).network2 L(0/2).RJVver_sure L(0/2).RJVver_nsure L(0/2).lrd2 L(1/1).m_lta L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(2 5)) dgmmiv(network, lag(2 5))  dgmmiv(network2, lag(2 5))  dgmmiv(RJVver_sure, lag(2 5)) dgmmiv(RJVver_nsure, lag(2 5)) dgmmiv(lrd2, lag(2 5))  lgmmiv(l(3/6).MS l(3/6).lrd2)  div(patents_t_2 l2.sales_new) div(L(1/1).m_lta L(1/1).m_lrd dy2-dy13) twostep vce(r)
estat abond
*xtdpd L(0/1).MS L(0/2).network L(0/2).network2 L(0/2).RJVver L(0/2).lrd2 L(1/1).m_lta L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0, dgmmiv(MS, lag(2 5)) dgmmiv(network, lag(2 5)) dgmmiv(network2, lag(2 5)) dgmmiv(RJVver, lag(2 5)) dgmmiv(lrd2, lag(2 5))  lgmmiv(l(3/6).MS l(3/6).lrd2)  div(patents_t_2 l2.sales_new) div(L(1/1).m_lta L(1/1).m_lrd dy2-dy13) twostep 
*estat sargan
*xtdpd L(0/1).MS L(0/2).network L(0/2).network2 L(0/2).RJVver L(0/2).lrd2 L(1/1).m_lta L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0, dgmmiv(MS, lag(2 5)) dgmmiv(network, lag(2 5)) dgmmiv(network2, lag(2 5)) dgmmiv(RJVver, lag(2 5)) dgmmiv(lrd2, lag(2 5))  lgmmiv(l(3/6).MS l(3/6).lrd2)  div(patents_t_2 l2.sales_new) div(L(1/1).m_lta L(1/1).m_lrd dy2-dy13) twostep vce(r)
*estat abond
gen eff_net=(_b[network]+_b[L.network]+_b[L2.network])*network+(_b[network2]+_b[L.network2]+_b[L2.network2])*network2
label var eff_net "Heterogenous effect (continuous)"


xtdpd L(0/1).MS L(0/2).network L(0/2).network2 L(0/2).RJVver_sure L(0/2).RJVver_nsure L(0/2).lrd2 L(1/1).m_lta L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(3 5)) dgmmiv(network, lag(3 5))  dgmmiv(network2, lag(3 5))  dgmmiv(RJVver_sure, lag(3 5)) dgmmiv(RJVver_nsure, lag(3 5)) dgmmiv(lrd2, lag(3 5))  lgmmiv(l(6/6).MS l(6/6).lrd2)  div(patents_t_2 l2.sales_new) div(L(1/1).m_lta L(1/1).m_lrd dy2-dy13) twostep vce(r)
estat abond

drop eff_net
gen eff_net=(_b[network]+_b[L.network]+_b[L2.network])*network+(_b[network2]+_b[L.network2]+_b[L2.network2])*network2
label var eff_net "Heterogenous effect (continuous)"
twoway (scatter eff_net network,  xlabel(0 .1 .2 .3 .4 .5 .6 .7 ) sort) if network<.75

*log using "graph_net_seg.log", replace
forvalues x=0(.01)1 {
lincom (network + L1.network + L2.network)*`x'+(network2 + L1.network2 + L2.network2)*`x'*`x', l(95)
}
*log close


esttab RJVseg_any RJVseg_hv RJVseg_sml using  "tables\REStat_MS14767_Vol96(2)_tables", append nodepvar star(* 0.10 ** 0.05 *** 0.01) se  stats(r N ) drop(dy*) label compress csv nogaps
esttab RJVseg_ver_sml using "tables\REStat_MS14767_Vol96(2)_tables", append nodepvar star(* 0.10 ** 0.05 *** 0.01) se  stats(r N ) drop(dy*) label compress csv nogaps
esttab RJVseg_sml_ms using  "tables\REStat_MS14767_Vol96(2)_tables", append nodepvar star(* 0.10 ** 0.05 *** 0.01) se  stats(r N ) drop(dy*) label compress csv nogaps

log close

